package com.hongkzh.www.friend.service;

import com.google.common.collect.Lists;
import com.hongkzh.www.adv.service.BaseService;
import com.hongkzh.www.conmon.CnToSpell;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.conmon.RongCloudTool;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.*;

public class FriendService extends BaseService {


    public Map<String,List<Record>> friendList(String id) {
        SqlPara sqlPara=Db.getSqlPara("friend.friendList",id);
        List<Record> friends= Db.find(sqlPara);
        Map<String,List<Record>> map=new TreeMap( new Comparator<String>() {
            @Override
            public int compare(String obj1, String obj2) {
                // 降序排序
                return obj1.compareTo(obj2);
            }
        });
        String letter=null;
        List<Record> a=null;
        for (Record r:friends
             ) {
            a=null;
            doFilePath(r);
            letter=CnToSpell.getPinYinHeadChar(r.get("name"));
            r.set("letter",letter);
            if(null==map.get(letter)){
                a=  Lists.newArrayList();
                a.add(r);
                map.put(letter,a);
            }
            else{
                a= map.get(letter);
                a.add(r);
                map.put(letter,a);
            }
        }

        return map;
    }

    public Map<String,List<Record>> circleList(String id) {
        SqlPara sqlPara=Db.getSqlPara("friend.circleList",id);
        List<Record> friends= Db.find(sqlPara);
        Map<String,List<Record>> map=new TreeMap( new Comparator<String>() {
            @Override
            public int compare(String obj1, String obj2) {
                // 降序排序
                return obj1.compareTo(obj2);
            }
        });
        String letter=null;
        List<Record> a=null;
        for (Record r:friends
                ) {
            a=null;
            doFilePath(r);
            letter=CnToSpell.getPinYinHeadChar(r.get("circleName"));
            r.set("letter",letter);
            if(null==map.get(letter)){
                a=  Lists.newArrayList();
                a.add(r);
                map.put(letter,a);
            }
            else{
                a= map.get(letter);
                a.add(r);
                map.put(letter,a);
            }


        }

        return map;
    }

    public void addFriend(String uid, String friendId) {
        SqlPara sql=Db.getSqlPara("user.getfriend",friendId,uid);
        List<Record> friends= Db.find(sql);
        //不是好友
        if(friends==null||friends.size()==0) {
            String id = IdGen.uuid();
            sql = Db.getSqlPara("user.friendAdd", id, friendId, uid);
            Db.update(sql);
        }
    }

    public void removeFriend(String uid, String friendId) {
        Db.delete(Db.getSql("friend.removeFriend"),   friendId, uid, uid,   friendId);
    }

    public void createGroup(String uid, String categoryId, String circleName, String coverImgSrc) throws Exception {
        String groupId = IdGen.uuid();
        SqlPara sql = Db.getSqlPara("friend.createGroup", groupId, categoryId, uid,circleName,coverImgSrc);
        Db.update(sql);
        RongCloudTool.createGroup( groupId,uid,  circleName);
        String id = IdGen.uuid();
        sql = Db.getSqlPara("friend.joinGroup",id, uid , groupId);
        Db.update(sql);
    }

    public void joinGroup(String uid, String circleId) throws Exception {
        SqlPara sql = Db.getSqlPara("friend.selectUserGroup", uid , circleId);
        Record record=Db.findFirst(sql);
        if(StrKit.notBlank(record.getStr("id"))) {
            String id = IdGen.uuid();
              sql = Db.getSqlPara("friend.joinGroup", id, uid, circleId);
            Db.update(sql);
            RongCloudTool.joinGroup(uid, circleId);
        }
    }

    public void quitGroup(String uid, String circleId) throws Exception {
        SqlPara sql = Db.getSqlPara("friend.quitGroup", uid , circleId);
        Db.update(sql);
        RongCloudTool.quitGroup(  uid,  circleId);
    }

    public String dismissGroup(String uid, String circleId) throws Exception {
        String result="不是圈主不能解除圈子";
        SqlPara sql = Db.getSqlPara("friend.selectGroup", uid , circleId);
        Record record=Db.findFirst(sql);
        if(StrKit.notBlank(record.getStr("id"))){
            sql = Db.getSqlPara("friend.dismissGroup", uid , circleId);
            Db.update(sql);
            sql = Db.getSqlPara("friend.dismissUserGroup",circleId);
            Db.update(sql);
            RongCloudTool.dismissGroup(  uid,  circleId);
            result="";
        }
       return result;
    }
}
